package leetcode.tree;

public class isBST {
    //暴力递归
    public static boolean process(Node head){
        if (head == null){
            return true;
        }
       if (head.left!=null && head.left.value>head.value){
           return false;
       }
       if (head.right!=null && head.right.value<head.value){
           return false;
       }
       if (!process(head.left) || !process(head.right)){
           return false;
       }
       return true;
    }
    //Morris遍历
    public static boolean process1(Node head){
        if (head == null){
            return true;
        }
        boolean res= true;
        Node pre = null;
        Node cur1 = head;
        Node cur2 = null;
        while (cur1 != null){
            cur2 = cur1.left;
            if (cur2 != null){
                while (cur2.right != null && cur2.right != cur1){
                    cur2 = cur2.right;
                }
                if (cur2.right == null){
                    cur2.right =cur1;
                    cur1 = cur1.left;
                    continue;
                }
                if (cur2.right == cur1){
                    cur2.right = null;
                }
            }
            if (pre != null && pre.value > cur1.value){
                res = false;
            }
            pre = cur1;
            cur1 = cur1.right;
        }
        return res;
    }
}
